<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/18
 * Time: 17:02
 */
namespace common\sources\read\taskcommod;


use common\models\craftsman\TaskCommod;

class TaskCommodAccess implements ITaskCommodAccess
{

    public function getTaskDetail($task_commod_id)
    {
        return TaskCommod::find()
                ->select("id,plat_type,shop_id,bus_id,shop_name,task_type,port_type,title,main_url1,main_url2,colour,size,price,num,task_answer,task_num,task_status,keywords,task_id,surplus_task_num,freight_deposit,
                prompt,com_amount,ship_addr,buy_get_commisse,commod_link,range_price,keywords,qr_code_url,return_type,shop_id,item_commisse,pay_task_commisse")
                ->where(["id"=> $task_commod_id])
                ->asArray()
                ->one();
    }

    public function getList($data)
    {
        $where = $this->getWhere($data);
        $task_commod = TaskCommod::find()
                ->alias("t")
                ->select("t.id,t.plat_type,t.shop_id,t.bus_id,t.shop_name,t.task_type,t.port_type,t.title,t.main_url1,t.main_url2,t.colour,t.size,t.price,t.num,t.task_answer,t.task_num,t.task_status,t.task_id,t.prompt,t.com_amount,t.ship_addr,t.freight_deposit,
                t.buy_get_commisse,t.range_price,t.return_type,t.avail_task_num,t.surplus_task_num,t.return_type")
                ->leftJoin("value_service v","t.id = v.task_commod_id")
                ->where($where);
             if($data['task_class'] == 3)
             {
                 $task_commod->orderBy("v.up_rank desc,t.pay_time asc");
             }else
             {
                 $task_commod->orderBy("t.create_time desc");
             }
        return $task_commod->offset(($data['page_num']-1)*$data['limit'])
                            ->limit($data['limit'])
                            ->asArray()
                            ->all();
    }
    //统计平台任务数量
    public function getTaskCommodCount($data)
    {
        $where = $this->getWhere($data);
        return TaskCommod::find()
                ->alias("t")
                ->where($where)
                ->count();
    }
    public function getWhere($data)
    {
        $sql = " t.public_task_status = 5 AND t.task_status = 4 AND t.surplus_task_num >0";
        if(!empty($data['plat_type']) && isset($data['plat_type']))
        {
            $sql .= " AND t.plat_type = " .$data['plat_type'] ;
        }
        if(!empty($data['task_type']) && isset($data['task_type']))
        {
            $sql .= " AND t.task_type = " .$data['task_type'] ;
        }
        if(!empty($data['port_type']) && isset($data['port_type']))
        {
            $sql .= " AND t.port_type = " .$data['port_type'] ;
        }
        if(!empty($data['task_class']) && isset($data['task_class']))
        {
            $sql .= " AND t.task_classify = " .$data['task_class'] ;
        }
        if(!empty($data['shop_ids']) && isset($data['shop_ids']))
        {
            $sql .= " AND t.shop_id not in ( " .$data['shop_ids']." )" ;
        }
        return $sql;
    }


}